package com.changgou.service.goods.service.impl;

import com.changgou.common.util.PinYinUtils;
import com.changgou.goods.pojo.Brand;
import com.changgou.service.goods.dao.BrandMapper;
import com.changgou.service.goods.service.BrandService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

/**
 * @Author: JoinCao
 * @CreateDate: 2020-03-13
 */
@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    private BrandMapper brandMapper;

    /**
     * @Author: JoinCao
     * @CreateDate: 2020-03-13
     * @Description: 品牌列表查询
     */
    @Override
    public List<Brand> findList() {
        List<Brand> brandList = brandMapper.selectAll();
        return brandList;
    }

    /**
     * @Author: JoinCao
     * @CreateDate: 2020-03-13
     * @Description: 根据id查询品牌数据
     */
    @Override
    public Brand findById(Integer id) {
        Brand brand = brandMapper.selectByPrimaryKey(id);
        return brand;
    }

    /**
     * @Author: JoinCao
     * @CreateDate: 2020-03-13
     * @Description: 品牌新增
     */
    @Transactional//对数据库有所改动需要声明事务管理
    @Override
    public void add(Brand brand) {
        //判断用户有没有输入首字母,若没有就自动生成
        if (StringUtils.isEmpty(brand.getLetter())) {
            String letter = PinYinUtils.getFirstLetter(brand.getName());
            brand.setLetter(letter);
        }
        //brandMapper.insert(brand);
        //通用mapper底层依然是拼字符串,自动拼,insertSelective与insert的区别在于前者不会拼属性值为null的属性
        brandMapper.insertSelective(brand);
    }

    /**
     * @Author: JoinCao
     * @CreateDate: 2020-03-13
     * @Description: 根据id删除品牌信息
     */
    @Transactional//对数据库有所改动需要声明事务管理
    @Override
    public void delById(Integer id) {
        brandMapper.deleteByPrimaryKey(id);
    }

    /**
     * @Author: JoinCao
     * @CreateDate: 2020-03-13
     * @Description: 品牌修改
     */
    @Transactional//对数据库有所改动需要声明事务管理
    @Override
    public void update(Brand brand) {
        brandMapper.updateByPrimaryKeySelective(brand);
    }

    /**
     * @Author: JoinCao
     * @CreateDate: 2020-03-13
     * @Description: 品牌列表条件查询
     */
    @Override
    public List<Brand> list(Map<String, Object> searchMap) {
        Example example = new Example(Brand.class);
        //封装查询条件
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            //根据品牌名称模糊查询
            if (searchMap.get("name") != null && !"".equals(searchMap.get("name"))) {
                criteria.andLike("name", "%" + searchMap.get("name") + "%");
            }
            //根据品牌首字母精确查询
            if (searchMap.get("letter") != null && !"".equals(searchMap.get("letter"))) {
                criteria.andEqualTo("letter", searchMap.get("letter"));
            }
        }
        //拼接成aql语句select*from tb_brand where name like '%%' and letter =
        List<Brand> brandList = brandMapper.selectByExample(example);
        return brandList;
    }

    /**
     * @Author: JoinCao
     * @CreateDate: 2020-03-13
     * @Description: 品牌列表分页查询
     */
    @Override
    public Page<Brand> findPage(int page, int size) {
        PageHelper.startPage(page, size);
        Page<Brand> brandPage = (Page<Brand>) brandMapper.selectAll();
        return brandPage;
    }

    /**
     * @Author: JoinCao
     * @CreateDate: 2020-03-14
     * @Description: 品牌列表分页+条件查询
     */
    @Override
    public Page<Brand> findPage(Map<String, Object> searchMap, int page, int size) {
        //1.设置分页查询
        PageHelper.startPage(page, size);
        //2.设置条件查询
        Example example = new Example(Brand.class);
        //封装查询条件
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            //根据品牌名称模糊查询
            if (searchMap.get("name") != null && !"".equals(searchMap.get("name"))) {
                criteria.andLike("name", "%" + searchMap.get("name") + "%");
            }
            //根据品牌首字母精确查询
            if (searchMap.get("letter") != null && !"".equals(searchMap.get("letter"))) {
                criteria.andEqualTo("letter", searchMap.get("letter"));
            }
        }
        //拼接成aql语句select*from tb_brand where name like '%%' and letter =
        Page<Brand> pageInfo = (Page<Brand>) brandMapper.selectByExample(example);
        return pageInfo;
    }
}
